/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador.autogenerats;

import controlador.autogenerats.exceptions.IllegalOrphanException;
import controlador.autogenerats.exceptions.NonexistentEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entity.UnitatsMesura;
import entity.Produccio;
import java.util.ArrayList;
import java.util.Collection;
import entity.Fitosanitari;
import entity.Collita;
import entity.Abono;

/**
 *
 * @author David
 */
public class UnitatsMesuraJpaController implements Serializable {

    public UnitatsMesuraJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(UnitatsMesura unitatsMesura) {
        if (unitatsMesura.getProduccioCollection() == null) {
            unitatsMesura.setProduccioCollection(new ArrayList<Produccio>());
        }
        if (unitatsMesura.getFitosanitariCollection() == null) {
            unitatsMesura.setFitosanitariCollection(new ArrayList<Fitosanitari>());
        }
        if (unitatsMesura.getFitosanitariCollection1() == null) {
            unitatsMesura.setFitosanitariCollection1(new ArrayList<Fitosanitari>());
        }
        if (unitatsMesura.getCollitaCollection() == null) {
            unitatsMesura.setCollitaCollection(new ArrayList<Collita>());
        }
        if (unitatsMesura.getAbonoCollection() == null) {
            unitatsMesura.setAbonoCollection(new ArrayList<Abono>());
        }
        if (unitatsMesura.getAbonoCollection1() == null) {
            unitatsMesura.setAbonoCollection1(new ArrayList<Abono>());
        }
        if (unitatsMesura.getUnitatsMesuraCollection() == null) {
            unitatsMesura.setUnitatsMesuraCollection(new ArrayList<UnitatsMesura>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            UnitatsMesura unitatsMesuraId = unitatsMesura.getUnitatsMesuraId();
            if (unitatsMesuraId != null) {
                unitatsMesuraId = em.getReference(unitatsMesuraId.getClass(), unitatsMesuraId.getId());
                unitatsMesura.setUnitatsMesuraId(unitatsMesuraId);
            }
            Collection<Produccio> attachedProduccioCollection = new ArrayList<Produccio>();
            for (Produccio produccioCollectionProduccioToAttach : unitatsMesura.getProduccioCollection()) {
                produccioCollectionProduccioToAttach = em.getReference(produccioCollectionProduccioToAttach.getClass(), produccioCollectionProduccioToAttach.getId());
                attachedProduccioCollection.add(produccioCollectionProduccioToAttach);
            }
            unitatsMesura.setProduccioCollection(attachedProduccioCollection);
            Collection<Fitosanitari> attachedFitosanitariCollection = new ArrayList<Fitosanitari>();
            for (Fitosanitari fitosanitariCollectionFitosanitariToAttach : unitatsMesura.getFitosanitariCollection()) {
                fitosanitariCollectionFitosanitariToAttach = em.getReference(fitosanitariCollectionFitosanitariToAttach.getClass(), fitosanitariCollectionFitosanitariToAttach.getId());
                attachedFitosanitariCollection.add(fitosanitariCollectionFitosanitariToAttach);
            }
            unitatsMesura.setFitosanitariCollection(attachedFitosanitariCollection);
            Collection<Fitosanitari> attachedFitosanitariCollection1 = new ArrayList<Fitosanitari>();
            for (Fitosanitari fitosanitariCollection1FitosanitariToAttach : unitatsMesura.getFitosanitariCollection1()) {
                fitosanitariCollection1FitosanitariToAttach = em.getReference(fitosanitariCollection1FitosanitariToAttach.getClass(), fitosanitariCollection1FitosanitariToAttach.getId());
                attachedFitosanitariCollection1.add(fitosanitariCollection1FitosanitariToAttach);
            }
            unitatsMesura.setFitosanitariCollection1(attachedFitosanitariCollection1);
            Collection<Collita> attachedCollitaCollection = new ArrayList<Collita>();
            for (Collita collitaCollectionCollitaToAttach : unitatsMesura.getCollitaCollection()) {
                collitaCollectionCollitaToAttach = em.getReference(collitaCollectionCollitaToAttach.getClass(), collitaCollectionCollitaToAttach.getId());
                attachedCollitaCollection.add(collitaCollectionCollitaToAttach);
            }
            unitatsMesura.setCollitaCollection(attachedCollitaCollection);
            Collection<Abono> attachedAbonoCollection = new ArrayList<Abono>();
            for (Abono abonoCollectionAbonoToAttach : unitatsMesura.getAbonoCollection()) {
                abonoCollectionAbonoToAttach = em.getReference(abonoCollectionAbonoToAttach.getClass(), abonoCollectionAbonoToAttach.getId());
                attachedAbonoCollection.add(abonoCollectionAbonoToAttach);
            }
            unitatsMesura.setAbonoCollection(attachedAbonoCollection);
            Collection<Abono> attachedAbonoCollection1 = new ArrayList<Abono>();
            for (Abono abonoCollection1AbonoToAttach : unitatsMesura.getAbonoCollection1()) {
                abonoCollection1AbonoToAttach = em.getReference(abonoCollection1AbonoToAttach.getClass(), abonoCollection1AbonoToAttach.getId());
                attachedAbonoCollection1.add(abonoCollection1AbonoToAttach);
            }
            unitatsMesura.setAbonoCollection1(attachedAbonoCollection1);
            Collection<UnitatsMesura> attachedUnitatsMesuraCollection = new ArrayList<UnitatsMesura>();
            for (UnitatsMesura unitatsMesuraCollectionUnitatsMesuraToAttach : unitatsMesura.getUnitatsMesuraCollection()) {
                unitatsMesuraCollectionUnitatsMesuraToAttach = em.getReference(unitatsMesuraCollectionUnitatsMesuraToAttach.getClass(), unitatsMesuraCollectionUnitatsMesuraToAttach.getId());
                attachedUnitatsMesuraCollection.add(unitatsMesuraCollectionUnitatsMesuraToAttach);
            }
            unitatsMesura.setUnitatsMesuraCollection(attachedUnitatsMesuraCollection);
            em.persist(unitatsMesura);
            if (unitatsMesuraId != null) {
                unitatsMesuraId.getUnitatsMesuraCollection().add(unitatsMesura);
                unitatsMesuraId = em.merge(unitatsMesuraId);
            }
            for (Produccio produccioCollectionProduccio : unitatsMesura.getProduccioCollection()) {
                UnitatsMesura oldUnitatsMesuraIdOfProduccioCollectionProduccio = produccioCollectionProduccio.getUnitatsMesuraId();
                produccioCollectionProduccio.setUnitatsMesuraId(unitatsMesura);
                produccioCollectionProduccio = em.merge(produccioCollectionProduccio);
                if (oldUnitatsMesuraIdOfProduccioCollectionProduccio != null) {
                    oldUnitatsMesuraIdOfProduccioCollectionProduccio.getProduccioCollection().remove(produccioCollectionProduccio);
                    oldUnitatsMesuraIdOfProduccioCollectionProduccio = em.merge(oldUnitatsMesuraIdOfProduccioCollectionProduccio);
                }
            }
            for (Fitosanitari fitosanitariCollectionFitosanitari : unitatsMesura.getFitosanitariCollection()) {
                UnitatsMesura oldUnitatsDisolucioOfFitosanitariCollectionFitosanitari = fitosanitariCollectionFitosanitari.getUnitatsDisolucio();
                fitosanitariCollectionFitosanitari.setUnitatsDisolucio(unitatsMesura);
                fitosanitariCollectionFitosanitari = em.merge(fitosanitariCollectionFitosanitari);
                if (oldUnitatsDisolucioOfFitosanitariCollectionFitosanitari != null) {
                    oldUnitatsDisolucioOfFitosanitariCollectionFitosanitari.getFitosanitariCollection().remove(fitosanitariCollectionFitosanitari);
                    oldUnitatsDisolucioOfFitosanitariCollectionFitosanitari = em.merge(oldUnitatsDisolucioOfFitosanitariCollectionFitosanitari);
                }
            }
            for (Fitosanitari fitosanitariCollection1Fitosanitari : unitatsMesura.getFitosanitariCollection1()) {
                UnitatsMesura oldUnitatsDosisOfFitosanitariCollection1Fitosanitari = fitosanitariCollection1Fitosanitari.getUnitatsDosis();
                fitosanitariCollection1Fitosanitari.setUnitatsDosis(unitatsMesura);
                fitosanitariCollection1Fitosanitari = em.merge(fitosanitariCollection1Fitosanitari);
                if (oldUnitatsDosisOfFitosanitariCollection1Fitosanitari != null) {
                    oldUnitatsDosisOfFitosanitariCollection1Fitosanitari.getFitosanitariCollection1().remove(fitosanitariCollection1Fitosanitari);
                    oldUnitatsDosisOfFitosanitariCollection1Fitosanitari = em.merge(oldUnitatsDosisOfFitosanitariCollection1Fitosanitari);
                }
            }
            for (Collita collitaCollectionCollita : unitatsMesura.getCollitaCollection()) {
                UnitatsMesura oldUnitatsMesuraIdOfCollitaCollectionCollita = collitaCollectionCollita.getUnitatsMesuraId();
                collitaCollectionCollita.setUnitatsMesuraId(unitatsMesura);
                collitaCollectionCollita = em.merge(collitaCollectionCollita);
                if (oldUnitatsMesuraIdOfCollitaCollectionCollita != null) {
                    oldUnitatsMesuraIdOfCollitaCollectionCollita.getCollitaCollection().remove(collitaCollectionCollita);
                    oldUnitatsMesuraIdOfCollitaCollectionCollita = em.merge(oldUnitatsMesuraIdOfCollitaCollectionCollita);
                }
            }
            for (Abono abonoCollectionAbono : unitatsMesura.getAbonoCollection()) {
                UnitatsMesura oldUnitatsQuantitatOfAbonoCollectionAbono = abonoCollectionAbono.getUnitatsQuantitat();
                abonoCollectionAbono.setUnitatsQuantitat(unitatsMesura);
                abonoCollectionAbono = em.merge(abonoCollectionAbono);
                if (oldUnitatsQuantitatOfAbonoCollectionAbono != null) {
                    oldUnitatsQuantitatOfAbonoCollectionAbono.getAbonoCollection().remove(abonoCollectionAbono);
                    oldUnitatsQuantitatOfAbonoCollectionAbono = em.merge(oldUnitatsQuantitatOfAbonoCollectionAbono);
                }
            }
            for (Abono abonoCollection1Abono : unitatsMesura.getAbonoCollection1()) {
                UnitatsMesura oldUnitatsPaquetOfAbonoCollection1Abono = abonoCollection1Abono.getUnitatsPaquet();
                abonoCollection1Abono.setUnitatsPaquet(unitatsMesura);
                abonoCollection1Abono = em.merge(abonoCollection1Abono);
                if (oldUnitatsPaquetOfAbonoCollection1Abono != null) {
                    oldUnitatsPaquetOfAbonoCollection1Abono.getAbonoCollection1().remove(abonoCollection1Abono);
                    oldUnitatsPaquetOfAbonoCollection1Abono = em.merge(oldUnitatsPaquetOfAbonoCollection1Abono);
                }
            }
            for (UnitatsMesura unitatsMesuraCollectionUnitatsMesura : unitatsMesura.getUnitatsMesuraCollection()) {
                UnitatsMesura oldUnitatsMesuraIdOfUnitatsMesuraCollectionUnitatsMesura = unitatsMesuraCollectionUnitatsMesura.getUnitatsMesuraId();
                unitatsMesuraCollectionUnitatsMesura.setUnitatsMesuraId(unitatsMesura);
                unitatsMesuraCollectionUnitatsMesura = em.merge(unitatsMesuraCollectionUnitatsMesura);
                if (oldUnitatsMesuraIdOfUnitatsMesuraCollectionUnitatsMesura != null) {
                    oldUnitatsMesuraIdOfUnitatsMesuraCollectionUnitatsMesura.getUnitatsMesuraCollection().remove(unitatsMesuraCollectionUnitatsMesura);
                    oldUnitatsMesuraIdOfUnitatsMesuraCollectionUnitatsMesura = em.merge(oldUnitatsMesuraIdOfUnitatsMesuraCollectionUnitatsMesura);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(UnitatsMesura unitatsMesura) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            UnitatsMesura persistentUnitatsMesura = em.find(UnitatsMesura.class, unitatsMesura.getId());
            UnitatsMesura unitatsMesuraIdOld = persistentUnitatsMesura.getUnitatsMesuraId();
            UnitatsMesura unitatsMesuraIdNew = unitatsMesura.getUnitatsMesuraId();
            Collection<Produccio> produccioCollectionOld = persistentUnitatsMesura.getProduccioCollection();
            Collection<Produccio> produccioCollectionNew = unitatsMesura.getProduccioCollection();
            Collection<Fitosanitari> fitosanitariCollectionOld = persistentUnitatsMesura.getFitosanitariCollection();
            Collection<Fitosanitari> fitosanitariCollectionNew = unitatsMesura.getFitosanitariCollection();
            Collection<Fitosanitari> fitosanitariCollection1Old = persistentUnitatsMesura.getFitosanitariCollection1();
            Collection<Fitosanitari> fitosanitariCollection1New = unitatsMesura.getFitosanitariCollection1();
            Collection<Collita> collitaCollectionOld = persistentUnitatsMesura.getCollitaCollection();
            Collection<Collita> collitaCollectionNew = unitatsMesura.getCollitaCollection();
            Collection<Abono> abonoCollectionOld = persistentUnitatsMesura.getAbonoCollection();
            Collection<Abono> abonoCollectionNew = unitatsMesura.getAbonoCollection();
            Collection<Abono> abonoCollection1Old = persistentUnitatsMesura.getAbonoCollection1();
            Collection<Abono> abonoCollection1New = unitatsMesura.getAbonoCollection1();
            Collection<UnitatsMesura> unitatsMesuraCollectionOld = persistentUnitatsMesura.getUnitatsMesuraCollection();
            Collection<UnitatsMesura> unitatsMesuraCollectionNew = unitatsMesura.getUnitatsMesuraCollection();
            List<String> illegalOrphanMessages = null;
            for (Produccio produccioCollectionOldProduccio : produccioCollectionOld) {
                if (!produccioCollectionNew.contains(produccioCollectionOldProduccio)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Produccio " + produccioCollectionOldProduccio + " since its unitatsMesuraId field is not nullable.");
                }
            }
            for (Fitosanitari fitosanitariCollectionOldFitosanitari : fitosanitariCollectionOld) {
                if (!fitosanitariCollectionNew.contains(fitosanitariCollectionOldFitosanitari)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Fitosanitari " + fitosanitariCollectionOldFitosanitari + " since its unitatsDisolucio field is not nullable.");
                }
            }
            for (Fitosanitari fitosanitariCollection1OldFitosanitari : fitosanitariCollection1Old) {
                if (!fitosanitariCollection1New.contains(fitosanitariCollection1OldFitosanitari)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Fitosanitari " + fitosanitariCollection1OldFitosanitari + " since its unitatsDosis field is not nullable.");
                }
            }
            for (Collita collitaCollectionOldCollita : collitaCollectionOld) {
                if (!collitaCollectionNew.contains(collitaCollectionOldCollita)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Collita " + collitaCollectionOldCollita + " since its unitatsMesuraId field is not nullable.");
                }
            }
            for (Abono abonoCollectionOldAbono : abonoCollectionOld) {
                if (!abonoCollectionNew.contains(abonoCollectionOldAbono)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Abono " + abonoCollectionOldAbono + " since its unitatsQuantitat field is not nullable.");
                }
            }
            for (Abono abonoCollection1OldAbono : abonoCollection1Old) {
                if (!abonoCollection1New.contains(abonoCollection1OldAbono)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Abono " + abonoCollection1OldAbono + " since its unitatsPaquet field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (unitatsMesuraIdNew != null) {
                unitatsMesuraIdNew = em.getReference(unitatsMesuraIdNew.getClass(), unitatsMesuraIdNew.getId());
                unitatsMesura.setUnitatsMesuraId(unitatsMesuraIdNew);
            }
            Collection<Produccio> attachedProduccioCollectionNew = new ArrayList<Produccio>();
            for (Produccio produccioCollectionNewProduccioToAttach : produccioCollectionNew) {
                produccioCollectionNewProduccioToAttach = em.getReference(produccioCollectionNewProduccioToAttach.getClass(), produccioCollectionNewProduccioToAttach.getId());
                attachedProduccioCollectionNew.add(produccioCollectionNewProduccioToAttach);
            }
            produccioCollectionNew = attachedProduccioCollectionNew;
            unitatsMesura.setProduccioCollection(produccioCollectionNew);
            Collection<Fitosanitari> attachedFitosanitariCollectionNew = new ArrayList<Fitosanitari>();
            for (Fitosanitari fitosanitariCollectionNewFitosanitariToAttach : fitosanitariCollectionNew) {
                fitosanitariCollectionNewFitosanitariToAttach = em.getReference(fitosanitariCollectionNewFitosanitariToAttach.getClass(), fitosanitariCollectionNewFitosanitariToAttach.getId());
                attachedFitosanitariCollectionNew.add(fitosanitariCollectionNewFitosanitariToAttach);
            }
            fitosanitariCollectionNew = attachedFitosanitariCollectionNew;
            unitatsMesura.setFitosanitariCollection(fitosanitariCollectionNew);
            Collection<Fitosanitari> attachedFitosanitariCollection1New = new ArrayList<Fitosanitari>();
            for (Fitosanitari fitosanitariCollection1NewFitosanitariToAttach : fitosanitariCollection1New) {
                fitosanitariCollection1NewFitosanitariToAttach = em.getReference(fitosanitariCollection1NewFitosanitariToAttach.getClass(), fitosanitariCollection1NewFitosanitariToAttach.getId());
                attachedFitosanitariCollection1New.add(fitosanitariCollection1NewFitosanitariToAttach);
            }
            fitosanitariCollection1New = attachedFitosanitariCollection1New;
            unitatsMesura.setFitosanitariCollection1(fitosanitariCollection1New);
            Collection<Collita> attachedCollitaCollectionNew = new ArrayList<Collita>();
            for (Collita collitaCollectionNewCollitaToAttach : collitaCollectionNew) {
                collitaCollectionNewCollitaToAttach = em.getReference(collitaCollectionNewCollitaToAttach.getClass(), collitaCollectionNewCollitaToAttach.getId());
                attachedCollitaCollectionNew.add(collitaCollectionNewCollitaToAttach);
            }
            collitaCollectionNew = attachedCollitaCollectionNew;
            unitatsMesura.setCollitaCollection(collitaCollectionNew);
            Collection<Abono> attachedAbonoCollectionNew = new ArrayList<Abono>();
            for (Abono abonoCollectionNewAbonoToAttach : abonoCollectionNew) {
                abonoCollectionNewAbonoToAttach = em.getReference(abonoCollectionNewAbonoToAttach.getClass(), abonoCollectionNewAbonoToAttach.getId());
                attachedAbonoCollectionNew.add(abonoCollectionNewAbonoToAttach);
            }
            abonoCollectionNew = attachedAbonoCollectionNew;
            unitatsMesura.setAbonoCollection(abonoCollectionNew);
            Collection<Abono> attachedAbonoCollection1New = new ArrayList<Abono>();
            for (Abono abonoCollection1NewAbonoToAttach : abonoCollection1New) {
                abonoCollection1NewAbonoToAttach = em.getReference(abonoCollection1NewAbonoToAttach.getClass(), abonoCollection1NewAbonoToAttach.getId());
                attachedAbonoCollection1New.add(abonoCollection1NewAbonoToAttach);
            }
            abonoCollection1New = attachedAbonoCollection1New;
            unitatsMesura.setAbonoCollection1(abonoCollection1New);
            Collection<UnitatsMesura> attachedUnitatsMesuraCollectionNew = new ArrayList<UnitatsMesura>();
            for (UnitatsMesura unitatsMesuraCollectionNewUnitatsMesuraToAttach : unitatsMesuraCollectionNew) {
                unitatsMesuraCollectionNewUnitatsMesuraToAttach = em.getReference(unitatsMesuraCollectionNewUnitatsMesuraToAttach.getClass(), unitatsMesuraCollectionNewUnitatsMesuraToAttach.getId());
                attachedUnitatsMesuraCollectionNew.add(unitatsMesuraCollectionNewUnitatsMesuraToAttach);
            }
            unitatsMesuraCollectionNew = attachedUnitatsMesuraCollectionNew;
            unitatsMesura.setUnitatsMesuraCollection(unitatsMesuraCollectionNew);
            unitatsMesura = em.merge(unitatsMesura);
            if (unitatsMesuraIdOld != null && !unitatsMesuraIdOld.equals(unitatsMesuraIdNew)) {
                unitatsMesuraIdOld.getUnitatsMesuraCollection().remove(unitatsMesura);
                unitatsMesuraIdOld = em.merge(unitatsMesuraIdOld);
            }
            if (unitatsMesuraIdNew != null && !unitatsMesuraIdNew.equals(unitatsMesuraIdOld)) {
                unitatsMesuraIdNew.getUnitatsMesuraCollection().add(unitatsMesura);
                unitatsMesuraIdNew = em.merge(unitatsMesuraIdNew);
            }
            for (Produccio produccioCollectionNewProduccio : produccioCollectionNew) {
                if (!produccioCollectionOld.contains(produccioCollectionNewProduccio)) {
                    UnitatsMesura oldUnitatsMesuraIdOfProduccioCollectionNewProduccio = produccioCollectionNewProduccio.getUnitatsMesuraId();
                    produccioCollectionNewProduccio.setUnitatsMesuraId(unitatsMesura);
                    produccioCollectionNewProduccio = em.merge(produccioCollectionNewProduccio);
                    if (oldUnitatsMesuraIdOfProduccioCollectionNewProduccio != null && !oldUnitatsMesuraIdOfProduccioCollectionNewProduccio.equals(unitatsMesura)) {
                        oldUnitatsMesuraIdOfProduccioCollectionNewProduccio.getProduccioCollection().remove(produccioCollectionNewProduccio);
                        oldUnitatsMesuraIdOfProduccioCollectionNewProduccio = em.merge(oldUnitatsMesuraIdOfProduccioCollectionNewProduccio);
                    }
                }
            }
            for (Fitosanitari fitosanitariCollectionNewFitosanitari : fitosanitariCollectionNew) {
                if (!fitosanitariCollectionOld.contains(fitosanitariCollectionNewFitosanitari)) {
                    UnitatsMesura oldUnitatsDisolucioOfFitosanitariCollectionNewFitosanitari = fitosanitariCollectionNewFitosanitari.getUnitatsDisolucio();
                    fitosanitariCollectionNewFitosanitari.setUnitatsDisolucio(unitatsMesura);
                    fitosanitariCollectionNewFitosanitari = em.merge(fitosanitariCollectionNewFitosanitari);
                    if (oldUnitatsDisolucioOfFitosanitariCollectionNewFitosanitari != null && !oldUnitatsDisolucioOfFitosanitariCollectionNewFitosanitari.equals(unitatsMesura)) {
                        oldUnitatsDisolucioOfFitosanitariCollectionNewFitosanitari.getFitosanitariCollection().remove(fitosanitariCollectionNewFitosanitari);
                        oldUnitatsDisolucioOfFitosanitariCollectionNewFitosanitari = em.merge(oldUnitatsDisolucioOfFitosanitariCollectionNewFitosanitari);
                    }
                }
            }
            for (Fitosanitari fitosanitariCollection1NewFitosanitari : fitosanitariCollection1New) {
                if (!fitosanitariCollection1Old.contains(fitosanitariCollection1NewFitosanitari)) {
                    UnitatsMesura oldUnitatsDosisOfFitosanitariCollection1NewFitosanitari = fitosanitariCollection1NewFitosanitari.getUnitatsDosis();
                    fitosanitariCollection1NewFitosanitari.setUnitatsDosis(unitatsMesura);
                    fitosanitariCollection1NewFitosanitari = em.merge(fitosanitariCollection1NewFitosanitari);
                    if (oldUnitatsDosisOfFitosanitariCollection1NewFitosanitari != null && !oldUnitatsDosisOfFitosanitariCollection1NewFitosanitari.equals(unitatsMesura)) {
                        oldUnitatsDosisOfFitosanitariCollection1NewFitosanitari.getFitosanitariCollection1().remove(fitosanitariCollection1NewFitosanitari);
                        oldUnitatsDosisOfFitosanitariCollection1NewFitosanitari = em.merge(oldUnitatsDosisOfFitosanitariCollection1NewFitosanitari);
                    }
                }
            }
            for (Collita collitaCollectionNewCollita : collitaCollectionNew) {
                if (!collitaCollectionOld.contains(collitaCollectionNewCollita)) {
                    UnitatsMesura oldUnitatsMesuraIdOfCollitaCollectionNewCollita = collitaCollectionNewCollita.getUnitatsMesuraId();
                    collitaCollectionNewCollita.setUnitatsMesuraId(unitatsMesura);
                    collitaCollectionNewCollita = em.merge(collitaCollectionNewCollita);
                    if (oldUnitatsMesuraIdOfCollitaCollectionNewCollita != null && !oldUnitatsMesuraIdOfCollitaCollectionNewCollita.equals(unitatsMesura)) {
                        oldUnitatsMesuraIdOfCollitaCollectionNewCollita.getCollitaCollection().remove(collitaCollectionNewCollita);
                        oldUnitatsMesuraIdOfCollitaCollectionNewCollita = em.merge(oldUnitatsMesuraIdOfCollitaCollectionNewCollita);
                    }
                }
            }
            for (Abono abonoCollectionNewAbono : abonoCollectionNew) {
                if (!abonoCollectionOld.contains(abonoCollectionNewAbono)) {
                    UnitatsMesura oldUnitatsQuantitatOfAbonoCollectionNewAbono = abonoCollectionNewAbono.getUnitatsQuantitat();
                    abonoCollectionNewAbono.setUnitatsQuantitat(unitatsMesura);
                    abonoCollectionNewAbono = em.merge(abonoCollectionNewAbono);
                    if (oldUnitatsQuantitatOfAbonoCollectionNewAbono != null && !oldUnitatsQuantitatOfAbonoCollectionNewAbono.equals(unitatsMesura)) {
                        oldUnitatsQuantitatOfAbonoCollectionNewAbono.getAbonoCollection().remove(abonoCollectionNewAbono);
                        oldUnitatsQuantitatOfAbonoCollectionNewAbono = em.merge(oldUnitatsQuantitatOfAbonoCollectionNewAbono);
                    }
                }
            }
            for (Abono abonoCollection1NewAbono : abonoCollection1New) {
                if (!abonoCollection1Old.contains(abonoCollection1NewAbono)) {
                    UnitatsMesura oldUnitatsPaquetOfAbonoCollection1NewAbono = abonoCollection1NewAbono.getUnitatsPaquet();
                    abonoCollection1NewAbono.setUnitatsPaquet(unitatsMesura);
                    abonoCollection1NewAbono = em.merge(abonoCollection1NewAbono);
                    if (oldUnitatsPaquetOfAbonoCollection1NewAbono != null && !oldUnitatsPaquetOfAbonoCollection1NewAbono.equals(unitatsMesura)) {
                        oldUnitatsPaquetOfAbonoCollection1NewAbono.getAbonoCollection1().remove(abonoCollection1NewAbono);
                        oldUnitatsPaquetOfAbonoCollection1NewAbono = em.merge(oldUnitatsPaquetOfAbonoCollection1NewAbono);
                    }
                }
            }
            for (UnitatsMesura unitatsMesuraCollectionOldUnitatsMesura : unitatsMesuraCollectionOld) {
                if (!unitatsMesuraCollectionNew.contains(unitatsMesuraCollectionOldUnitatsMesura)) {
                    unitatsMesuraCollectionOldUnitatsMesura.setUnitatsMesuraId(null);
                    unitatsMesuraCollectionOldUnitatsMesura = em.merge(unitatsMesuraCollectionOldUnitatsMesura);
                }
            }
            for (UnitatsMesura unitatsMesuraCollectionNewUnitatsMesura : unitatsMesuraCollectionNew) {
                if (!unitatsMesuraCollectionOld.contains(unitatsMesuraCollectionNewUnitatsMesura)) {
                    UnitatsMesura oldUnitatsMesuraIdOfUnitatsMesuraCollectionNewUnitatsMesura = unitatsMesuraCollectionNewUnitatsMesura.getUnitatsMesuraId();
                    unitatsMesuraCollectionNewUnitatsMesura.setUnitatsMesuraId(unitatsMesura);
                    unitatsMesuraCollectionNewUnitatsMesura = em.merge(unitatsMesuraCollectionNewUnitatsMesura);
                    if (oldUnitatsMesuraIdOfUnitatsMesuraCollectionNewUnitatsMesura != null && !oldUnitatsMesuraIdOfUnitatsMesuraCollectionNewUnitatsMesura.equals(unitatsMesura)) {
                        oldUnitatsMesuraIdOfUnitatsMesuraCollectionNewUnitatsMesura.getUnitatsMesuraCollection().remove(unitatsMesuraCollectionNewUnitatsMesura);
                        oldUnitatsMesuraIdOfUnitatsMesuraCollectionNewUnitatsMesura = em.merge(oldUnitatsMesuraIdOfUnitatsMesuraCollectionNewUnitatsMesura);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = unitatsMesura.getId();
                if (findUnitatsMesura(id) == null) {
                    throw new NonexistentEntityException("The unitatsMesura with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            UnitatsMesura unitatsMesura;
            try {
                unitatsMesura = em.getReference(UnitatsMesura.class, id);
                unitatsMesura.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The unitatsMesura with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Produccio> produccioCollectionOrphanCheck = unitatsMesura.getProduccioCollection();
            for (Produccio produccioCollectionOrphanCheckProduccio : produccioCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This UnitatsMesura (" + unitatsMesura + ") cannot be destroyed since the Produccio " + produccioCollectionOrphanCheckProduccio + " in its produccioCollection field has a non-nullable unitatsMesuraId field.");
            }
            Collection<Fitosanitari> fitosanitariCollectionOrphanCheck = unitatsMesura.getFitosanitariCollection();
            for (Fitosanitari fitosanitariCollectionOrphanCheckFitosanitari : fitosanitariCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This UnitatsMesura (" + unitatsMesura + ") cannot be destroyed since the Fitosanitari " + fitosanitariCollectionOrphanCheckFitosanitari + " in its fitosanitariCollection field has a non-nullable unitatsDisolucio field.");
            }
            Collection<Fitosanitari> fitosanitariCollection1OrphanCheck = unitatsMesura.getFitosanitariCollection1();
            for (Fitosanitari fitosanitariCollection1OrphanCheckFitosanitari : fitosanitariCollection1OrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This UnitatsMesura (" + unitatsMesura + ") cannot be destroyed since the Fitosanitari " + fitosanitariCollection1OrphanCheckFitosanitari + " in its fitosanitariCollection1 field has a non-nullable unitatsDosis field.");
            }
            Collection<Collita> collitaCollectionOrphanCheck = unitatsMesura.getCollitaCollection();
            for (Collita collitaCollectionOrphanCheckCollita : collitaCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This UnitatsMesura (" + unitatsMesura + ") cannot be destroyed since the Collita " + collitaCollectionOrphanCheckCollita + " in its collitaCollection field has a non-nullable unitatsMesuraId field.");
            }
            Collection<Abono> abonoCollectionOrphanCheck = unitatsMesura.getAbonoCollection();
            for (Abono abonoCollectionOrphanCheckAbono : abonoCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This UnitatsMesura (" + unitatsMesura + ") cannot be destroyed since the Abono " + abonoCollectionOrphanCheckAbono + " in its abonoCollection field has a non-nullable unitatsQuantitat field.");
            }
            Collection<Abono> abonoCollection1OrphanCheck = unitatsMesura.getAbonoCollection1();
            for (Abono abonoCollection1OrphanCheckAbono : abonoCollection1OrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This UnitatsMesura (" + unitatsMesura + ") cannot be destroyed since the Abono " + abonoCollection1OrphanCheckAbono + " in its abonoCollection1 field has a non-nullable unitatsPaquet field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            UnitatsMesura unitatsMesuraId = unitatsMesura.getUnitatsMesuraId();
            if (unitatsMesuraId != null) {
                unitatsMesuraId.getUnitatsMesuraCollection().remove(unitatsMesura);
                unitatsMesuraId = em.merge(unitatsMesuraId);
            }
            Collection<UnitatsMesura> unitatsMesuraCollection = unitatsMesura.getUnitatsMesuraCollection();
            for (UnitatsMesura unitatsMesuraCollectionUnitatsMesura : unitatsMesuraCollection) {
                unitatsMesuraCollectionUnitatsMesura.setUnitatsMesuraId(null);
                unitatsMesuraCollectionUnitatsMesura = em.merge(unitatsMesuraCollectionUnitatsMesura);
            }
            em.remove(unitatsMesura);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<UnitatsMesura> findUnitatsMesuraEntities() {
        return findUnitatsMesuraEntities(true, -1, -1);
    }

    public List<UnitatsMesura> findUnitatsMesuraEntities(int maxResults, int firstResult) {
        return findUnitatsMesuraEntities(false, maxResults, firstResult);
    }

    private List<UnitatsMesura> findUnitatsMesuraEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(UnitatsMesura.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public UnitatsMesura findUnitatsMesura(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(UnitatsMesura.class, id);
        } finally {
            em.close();
        }
    }

    public int getUnitatsMesuraCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<UnitatsMesura> rt = cq.from(UnitatsMesura.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
